//#include "Variable_Function.h"

//#include <fcntl.h>
//#include <errno.h>
//#include <sys/mman.h>
//#include <stdio.h>
//#include <sys/ioctl.h>
//#include <string.h>
//#include <stdlib.h>
//#include <assert.h>

//#include <sys/socket.h>


//#include <sys/time.h>
//#include <sys/types.h>
//#include <sys/socket.h>
//#include <sys/select.h>
//#include <unistd.h>
//#include <arpa/inet.h>

//#pragma pack(1)

//struct ST_TCP_CTR_CMD
//{
//    unsigned char head;
//    unsigned char head1;
//    unsigned char head2;
//    unsigned char ctr;
//    short x1;
//    short y1;
//    short rangX;
//    short rangY;
//    unsigned char crc;
//    unsigned char end;
//}TCP_CTR_CMD;


//extern int TCP_Accept_CMD_fd;
//extern int TCP_Accept_VIDEO_fd;
//extern int UART3_fd;


//#if 1

//Mat last_gray;
//Mat last_gray_temp;
//Mat current_gray;
//Mat current_gray_temp;
//Rect box_b;
//Rect box_rang;

//Mat first;
//FILE *bb_file;
//bool tl1;

////视频源

//int Rec_Target_x = 1920;
//int Rec_Target_y = 1080;
//int mubiaodiushi = 1;
////int CCD_IR_Target_x = 0;
////int CCD_IR_Target_y = 0;
//unsigned short CCD_IR_Target_x = 360, CCD_IR_Target_y = 288;
//unsigned short CCD_IR_Detect_x = 360, CCD_IR_Detect_y = 288;
//int relDistanceX = 0;
//int relDistanceY = 0;

////tld
//extern int target_x;
//extern int target_y;
//extern int sockfd;
//unsigned char gSubImageData_malloc[1920*1080*3/2] = {0};
//double tcp_sent_time = 0;

//#define H_HEIGHT 720
//#define W_WIDTH  1280

//extern Mat rotateImg;
//BoundingBox draw_pbox;
//int tar_centor_x = 0;
//int tar_centor_y = 0;
//bool status;

//int TLD_IMAGE_TRACK(int Frame_NUM)
//{
//	// 跟踪开始
//	// 跟踪初始化
//	if(Frame_NUM == 0)
//	{
//        clearr();
//        //读取参数 可用于调试
//		FileStorage fs;
//		fs.open("parameters.yml", FileStorage::READ);
//		read(fs.getFirstTopLevelNode());
//		fs.release();
		
//		tl1 = true;
//        last_gray    = Mat::zeros(H_HEIGHT, W_WIDTH, CV_8UC1);
//        current_gray = Mat::zeros(H_HEIGHT, W_WIDTH, CV_8UC1);
////        current_gray_temp = Mat::zeros(1080, 1920, CV_8UC1);
//		bb_file = fopen("bounding_boxes.txt", "w");

////        memcpy(last_gray.data, gSubImageData_malloc, 1920*1080);
//        //memcpy(last_gray.data, gSubImageData_malloc, W_WIDTH*H_HEIGHT);
////        rotateImg.copyTo(last_gray);


//        box_b.x = 48;
//        box_b.y = 48;
//        box_b.width  = 32;
//        box_b.height = 32;

//        //搜索中心和目标中心重合
//        CCD_IR_Target_x = target_x;
//        CCD_IR_Target_y = target_y;
//        CCD_IR_Detect_x = target_x;
//        CCD_IR_Detect_y = target_y;
//        if(CCD_IR_Detect_x < 64)
//        {
//            CCD_IR_Detect_x = 65;
//        }
//        if(CCD_IR_Detect_y < 64)
//        {
//            CCD_IR_Detect_y = 65;
//        }
//        if(CCD_IR_Detect_x + 64 > W_WIDTH)
//        {
//            CCD_IR_Detect_x = W_WIDTH - 65;
//        }
//        if(CCD_IR_Detect_y + 64 > H_HEIGHT)
//        {
//            CCD_IR_Detect_y = H_HEIGHT - 65;
//        }

//        //初始化时，检测中心和跟踪框中心重合，相对位置固定48
//        relDistanceX = 0;
//        relDistanceY = 0;
//        Mat last_gray_resize;
//        last_gray_resize = last_gray(Rect(target_x - 64, target_y - 64, 128, 128));

//        double t = (double)getTickCount();
//        init(last_gray_resize, box_b, bb_file);
//        t=(double)getTickCount()-t;
//        printf("xiangang----------------------------------->init Run-time: %gms\n", t*1000/getTickFrequency());
//imwrite("box.bmp",last_gray_resize(box_b));
//imwrite("last_gray_resize.bmp",last_gray_resize);

//	}
//	// 跟踪 检测
//	if(Frame_NUM >= 1)
//	{
//        double t_track = (double)getTickCount();

////        memcpy(current_gray.data, gSubImageData_malloc,  W_WIDTH*H_HEIGHT);
//        rotateImg.copyTo(current_gray);

//		BoundingBox pbox;
////		BoundingBox draw_pbox;
//		std::vector<Point2f> pts1;
//		std::vector<Point2f> pts2;
//        status = true;

//		processFrame(last_gray, current_gray, pts1, pts2, pbox, status, tl1, bb_file);
//		mubiaodiushi = 1;
//		if(status)
//		{

//            // 新框x = 检测框x + 相对搜索框位置x
//            // 相对搜索框位置x = CCD_IR_Target_x中心x - 64
//            draw_pbox.x = pbox.x + CCD_IR_Detect_x - 64;
//            draw_pbox.y = pbox.y + CCD_IR_Detect_y - 64;
//            draw_pbox.width = pbox.width;
//            draw_pbox.height = pbox.height;
//            mubiaodiushi = 0;

//            tar_centor_x = draw_pbox.x;

//            //目标框中心
//            CCD_IR_Target_x = draw_pbox.x + draw_pbox.width / 2;
//            CCD_IR_Target_y = draw_pbox.y + draw_pbox.height / 2;

//            //搜索框中心
//            if(abs(CCD_IR_Target_x - CCD_IR_Detect_x) > 20 ||
//                    abs(CCD_IR_Target_y - CCD_IR_Detect_y) > 20)
//            {
//               //画框，中心点， 框大小为128x128
//                CCD_IR_Detect_x = CCD_IR_Target_x;
//                CCD_IR_Detect_y = CCD_IR_Target_y;
//                if(CCD_IR_Detect_x < 64)
//                {
//                    CCD_IR_Detect_x = 65;
//                }
//                if(CCD_IR_Detect_y < 64)
//                {
//                    CCD_IR_Detect_y = 65;
//                }

//                if(CCD_IR_Detect_x + 64 > W_WIDTH)
//                {
//                    CCD_IR_Detect_x = W_WIDTH - 65;
//                }
//                if(CCD_IR_Detect_y + 64 > H_HEIGHT)
//                {
//                    CCD_IR_Detect_y = H_HEIGHT - 65;
//                }
//            }

//            //当 搜索框中心 变化时，重新计算检测出的 目标框中心 相对位置
//            relDistanceX = (CCD_IR_Target_x - 16) - (CCD_IR_Detect_x - 64);
//            relDistanceY = (CCD_IR_Target_y - 16) - (CCD_IR_Detect_y - 64);

//            t_track = (double)getTickCount() - t_track;
//            double use_time = t_track*1000/getTickFrequency();
//            printf("xiangang----------------------------------->all run-time: %gms\n", use_time);

////            tcp_sent_time =(double)getTickCount() - tcp_sent_time;
////            printf("xiangang----------------------------------->tcp_sent_time Run-time: %gms\n", tcp_sent_time*1000/getTickFrequency());
////            sendTargetPoint2Pc(CCD_IR_Target_x, CCD_IR_Target_y, CCD_IR_Detect_x, CCD_IR_Detect_y, (int)use_time);
////            tcp_sent_time = (double)getTickCount();
//		}

//		swap(last_gray, current_gray);
//		pts2.clear();
//		pts1.clear();

////        t_track = (double)getTickCount() - t_track;
////        printf("xiangang----------------------------------->all run-time: %gms\n", t_track*1000/getTickFrequency());
//	}
//}

//#endif
